package com.itgungnir.designpattern.composite;

import java.util.List;

public class Test {
	public static void main(String[] args) {
		// 初始化文件结构树
		MyFile rootDir = initFileTree();
		// 按层级打印文件结构树
		printFileInfo(rootDir);
	}

	// 初始化文件结构树
	private static MyFile initFileTree() {
		MyFile rootDir = new MyFile("rootDir", true, 100, 0);

		MyFile subDir1_1 = new MyFile("subDir1_1", true, 50, 1);
		MyFile subFile2_1_1 = new MyFile("subFile2_1_1", false, 10, 2);
		MyFile subFile2_1_2 = new MyFile("subFile2_1_2", false, 15, 2);
		MyFile subDir2_1_1 = new MyFile("subDir2_1_1", true, 25, 2);
		MyFile subDir2_1_2 = new MyFile("subDir2_1_2", true, 0, 2);
		MyFile subFile3_1_1_1 = new MyFile("subFile3_1_1_1", false, 19, 3);
		MyFile subFile3_1_1_2 = new MyFile("subFile3_1_1_2", false, 6, 3);
		subDir2_1_1.addSubFile(subFile3_1_1_1);
		subDir2_1_1.addSubFile(subFile3_1_1_2);
		subDir1_1.addSubFile(subFile2_1_1);
		subDir1_1.addSubFile(subFile2_1_2);
		subDir1_1.addSubFile(subDir2_1_1);
		subDir1_1.addSubFile(subDir2_1_2);
		rootDir.addSubFile(subDir1_1);

		MyFile subDir1_2 = new MyFile("subDir1_2", true, 30, 1);
		MyFile subFile2_2_1 = new MyFile("subFile2_2_1", false, 22, 2);
		MyFile subFile2_2_2 = new MyFile("subFile2_2_2", false, 8, 2);
		subDir1_2.addSubFile(subFile2_2_1);
		subDir1_2.addSubFile(subFile2_2_2);
		rootDir.addSubFile(subDir1_2);

		MyFile subDir1_3 = new MyFile("subDir1_3", true, 20, 1);
		MyFile subDir2_3_1 = new MyFile("subDir2_3_1", true, 20, 2);
		MyFile subDir3_3_1_1 = new MyFile("subDir2_3_1_1", true, 20, 3);
		MyFile subDir4_3_1_1_1 = new MyFile("subDir2_3_1_1_1", true, 20, 4);
		MyFile subFile5_3_1_1_1_1 = new MyFile("subFile2_3_1_1_1_1", false, 20, 5);
		subDir4_3_1_1_1.addSubFile(subFile5_3_1_1_1_1);
		subDir3_3_1_1.addSubFile(subDir4_3_1_1_1);
		subDir2_3_1.addSubFile(subDir3_3_1_1);
		subDir1_3.addSubFile(subDir2_3_1);
		rootDir.addSubFile(subDir1_3);

		return rootDir;
	}

	// 递归打印文件树信息
	private static void printFileInfo(MyFile file) {
		System.out.println(file.toString());
		List<MyFile> subFiles = file.getSubFiles();
		if (subFiles != null && subFiles.size() > 0) {
			for (MyFile subFile : subFiles) {
				printFileInfo(subFile);
			}
		} else {
		}
	}
}